package cn.tedu.ivoscm.vehicle.controller;

import cn.tedu.ivoscm.base.response.JsonResult;
import cn.tedu.ivoscm.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivoscm.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivoscm.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivoscm.vehicle.service.VehicleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/vehicle")
@Api(tags = "02.车辆管理")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @GetMapping("select")
    @ApiOperation(value = "查询车辆信息")
    @ApiOperationSupport(order = 10)
    public JsonResult select(VehicleQuery vq) {
        log.debug("开始处理查询车辆列表请求，参数：{}", vq);
        List<VehicleVO> list = vehicleService.select(vq);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    @ApiOperation(value = "保存车辆信息")
    @ApiOperationSupport(order = 20)
    public JsonResult save(@RequestBody @Validated VehicleSaveParam vsp) {
        log.debug("开始处理保存车辆信息请求，参数：{}", vsp);
        vehicleService.save(vsp);
        return JsonResult.ok();
    }

    @PostMapping("unbindGeofence/{vid}")
    @ApiOperation(value = "解除某个车辆与围栏的绑定")
    @ApiOperationSupport(order = 21)
    public JsonResult unbindGeofence(@PathVariable Long vid) {
        log.debug("开始处理解除车辆围栏绑定请求，参数：{}", vid);
        vehicleService.unbind(vid);
        return JsonResult.ok();
    }

    @PostMapping("bindGeofence/{vid}/{gid}")
    @ApiOperation(value = "设置车辆围栏绑定")
    @ApiOperationSupport(order = 22)
    public JsonResult bindGeofence(@PathVariable Long vid, @PathVariable Long gid) {
        log.debug("开始处理设置车辆围栏绑定请求，参数：车辆id {}, 围栏id {}", vid, gid);
        vehicleService.bind(vid, gid);
        return JsonResult.ok();
    }

    @PostMapping("unbindAllThisGid/{gid}")
    @ApiOperation(value = "解除与某个围栏绑定的全部车辆")
    @ApiOperationSupport(order = 21)
    public JsonResult unbindAllThisGid(@PathVariable Long gid) {
        log.debug("开始处理解除车辆围栏绑定请求，参数：{}", gid);
        vehicleService.unbindAllThisGid(gid);
        return JsonResult.ok();
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除车辆信息")
    @ApiOperationSupport(order = 30)
    public JsonResult delete(@PathVariable Long id) {
        log.debug("开始处理删除车辆信息请求，参数：{}", id);
        vehicleService.deleteById(id);
        return JsonResult.ok();
    }


}
